<?php
/**
 * $Id: response.php 18 2009-06-08 07:59:38Z eyou80 $
 */

/**
 * 服务器响应类
 *
 * @package Response
 */
class Tinyask_Response
{
	/**
	 * HTTP CODE
	 *
	 * @var array
	 */
	private static $_code = array(
		100 => 'Continue',
		101 => 'Switching Protocols',
		200 => 'OK',
		201 => 'Created',
		202 => 'Accepted',
		203 => 'Non-Authoritative Information',
		204 => 'No Content',
		205 => 'Reset Content',
		206 => 'Partial Content',
		300 => 'Multiple Choices',
		301 => 'Moved Permanently',
		302 => 'Found',
		303 => 'See Other',
		304 => 'Not Modified',
		305 => 'Use Proxy',
		307 => 'Temporary Redirect',
		400 => 'Bad Request',
		401 => 'Unauthorized',
		402 => 'Payment Required',
		403 => 'Forbidden',
		404 => 'Not Found',
		405 => 'Method Not Allowed',
		406 => 'Not Acceptable',
		407 => 'Proxy Authentication Required',
		408 => 'Request Timeout',
		409 => 'Conflict',
		410 => 'Gone',
		411 => 'Length Required',
		412 => 'Precondition Failed',
		413 => 'Request Entity Too Large',
		414 => 'Request-URI Too Long',
		415 => 'Unsupported Media Type',
		416 => 'Requested Range Not Satisfiable',
		417 => 'Expectation Failed',
		500 => 'Internal Server Error',
		501 => 'Not Implemented',
		502 => 'Bad Gateway',
		503 => 'Service Unavailable',
		504 => 'Gateway Timeout',
		505 => 'HTTP Version Not Supported'
	);

	/**
	 * 默认字符集
	 *
	 * @var string
	 */
	private static $_charset = 'UTF-8';

	/**
	 * 设置字符集
	 *
	 * @param string $charset
	 */
	public static function setCharset($charset)
	{
		self::$_charset = $charset;
	}

	/**
	 * 声明内容类型和字符集
	 *
	 * @param string $contentType
	 * @param string $charset
	 */
	public static function setContentType($contentType = 'text/html', $charset = null)
	{
		header('content-Type: ' . $contentType . '; charset=' . (empty($charset) ? self::$_charset : $charset), true);
	}

	/**
	 * 设置HTTP头
	 *
	 * @param string $name 名称
	 * @param string $value 值
	 * @param boolean $replace 是否替换重复
	 * @param integer $responseCode 返回代码
	 */
	public static function setHeader($name, $value, $replace = false, $responseCode = 0)
	{
		if($responseCode > 0) {
			header($name . ': ' . $value, $replace, $responseCode);
		} else {
			header($name . ': ' . $value, $replace);
		}
	}

	/**
	 * 设置HTTP状态代码
	 *
	 * @param integer $code
	 */
	public static function setStatus($code)
	{
		if(isset(self::$_code[$code])) {
			header('HTTP/1.1 ' . $code . ' ' . self::$_code[$code], true, $code);
		}
	}

	/**
	 * 重定向
	 *
	 * @param string $location
	 * @param boolean $isPermanently
	 */
	public static function redirect($location, $isPermanently = false)
	{
		if($isPermanently) {
			self::setHeader('location', $location, false, 301);
			exit('Goto: "' . $location . '", Code: 301');
		} else {
			self::setHeader('location', $location, false, 302);
			exit('Goto: "' . $location . '", Code: 302');
		}
	}

	/**
	 * 设置指定的COOKIE值
	 *
	 * @param string $name
	 * @param mixed $value
	 * @param integer $expire
	 * @param string $url
	 */
	public static function setCookie($name, $value, $expire = 0, $url = null)
	{
		$path = '/';
		if(!empty($url)) {
			$parsed = parse_url($url);

			/** 在路径后面强制加上斜杠 */
			$path = empty($parsed['path']) ? '/' : Tinyask_Common::url(null, $parsed['path']);
		}

		if(is_array($value)) {
			foreach ($value as $k => $v) {
				setcookie("{$name}[{$k}]", $v, $expire, $path);
			}
		}  else {
			setcookie($name, $value, $expire, $path);
		}
	}

	/**
	 * 删除指定的COOKIE
	 *
	 * @param string $name
	 * @param string $url
	 */
	public static function delCookie($name, $url = null)
	{
		if(isset($_COOKIE[$name])) {
			$path = '/';
			if(!empty($url)) {
				$parsed = parse_url($url);

				/** 在路径后面强制加上斜杠 */
				$path = empty($parsed['path']) ? '/' : Tinyask_Common::url(null, $parsed['path']);
			}

			if(is_array($_COOKIE[$name])) {
				foreach ($_COOKIE[$name] as $k => $v) {
					setcookie("{$name}[{$k}]", '', time() - 2592000, $path);
				}
			} else {
				setcookie($name, '', time() - 2592000, $path);
			}
		}

	}

	/**
	 * 设置指定的SESSION值
	 *
	 * @param string $name
	 * @param mixed $value
	 */
	public static function setSession($name, $value)
	{
		$_SESSION[$name] = $value;
	}

	/**
	 * 删除指定的SESSION
	 *
	 * @param string $name
	 */
	public static function delSession($name)
	{
		if(isset($_SESSION[$name])) {
			unset($_SESSION[$name]);
		}
	}
}
